
module Account
    
    module  AccountNotify
        
        def signup(user, url=nil)
            
            setup_email(user)
            
            # Email header info
            @subject += "Welcome to #{AccountConfiguration.config(:app_name)}!"
            
            # Email body substitutions
            @body["name"] = "#{user.firstname} #{user.lastname}"
            @body["login"] = user.login
            @body["url"] = url || AccountConfiguration.config(:app_url).to_s
            @body["app_name"] = AccountConfiguration.config(:app_name).to_s
        end
        
        def reset_password(user)
            setup_email(user)
            @subject    += 'Your password has been reset'
        end
        
        def forgot_password(user, url=nil)
            setup_email(user)
            
            # Email header info
            @subject += "Forgotten password notification"
            
            # Email body substitutions
            @body["name"] = "#{user.firstname} #{user.lastname}"
            @body["login"] = user.login
            @body["url"] = url || AccountConfiguration.config(:app_url).to_s
            @body["app_name"] = AccountConfiguration.config(:app_name).to_s
        end
        
        def change_password(user, password, url=nil)
            setup_email(user)
            
            # Email header info
            @subject += "Changed password notification"
            
            # Email body substitutions
            @body["name"] = "#{user.firstname} #{user.lastname}"
            @body["login"] = user.login
            @body["password"] = password
            @body["url"] = url || AccountConfiguration.config(:app_url).to_s
            @body["app_name"] = AccountConfiguration.config(:app_name).to_s
        end
       
        protected 
        def setup_email(user)
            self.raise_delivery_errors = true
            @recipients = "#{user.email}"
            @from       = AccountConfiguration.config(:email_from).to_s
            @subject    = "[#{AccountConfiguration.config(:app_name)}] "
            @sent_on    = Time.now
            @headers['Content-Type'] = "text/plain; charset=#{AccountConfiguration.config(:mail_charset)}; format=flowed"
        end
    end
    
end
